import requests
import json

# DeepSeek API 配置
DEEPSEEK_API_KEY = "sk-079f9ad2ad3f457ebd6e6eb90f56fb53"  # 替换为您的实际API密钥
DEEPSEEK_API_URL = "https://api.deepseek.com/v1/chat/completions"

def get_deepseek_completion(prompt, model="deepseek-chat"):
    """使用 DeepSeek API 获取回复"""
    headers = {
        "Authorization": f"Bearer {DEEPSEEK_API_KEY}",
        "Content-Type": "application/json"
    }

    payload = {
        "model": model,
        "messages": [{"role": "user", "content": prompt}],
        "temperature": 0
    }

    try:
        response = requests.post(DEEPSEEK_API_URL, headers=headers, json=payload)
        response.raise_for_status()  # 检查HTTP错误

        result = response.json()
        return result['choices'][0]['message']['content']
    except requests.exceptions.RequestException as e:
        print(f"API请求错误: {e}")
        return None
    except (KeyError, IndexError) as e:
        print(f"解析响应错误: {e}")
        return None

customer_email = """
Arrr, I be fuming that me blender lid \
flew off and splattered me kitchen walls \
with smoothie! And to make matters worse,\
the warranty don't cover the cost of \
cleaning up me kitchen. I need yer help \
right now, matey!
"""

style = """American English \
in a calm and respectful tone
"""

prompt = f"""Translate the text \
that is delimited by triple backticks 
into a style that is {style}.
text: ```{customer_email}```
"""

# 获取并打印翻译结果
translation = get_deepseek_completion(prompt)
if translation:
    print("\n翻译结果:")
    print(translation)